
###################################
## Descriptive survey data clean ##
###################################

surv <- read.csv("data/lucid descriptive data.csv", colClasses = "character")[c(-1,-2),]

## rename questions
names(surv)[c(2:3,
   20,
   21:24)] <- 
  c("party","ideology",
  "employment",
  "trade_affect","offshore_affect","isolationism","nationalism")

## some respondents did not take survey after reading consent
surv <- surv[surv$Q1 == "Yes",]

# some respondents never finished
drops <- surv$TradeOpenFavor == "" | surv$TradeAgreementFavor == ""
surv <- surv[drops == FALSE,]

## reassign answers for repeated questions to final answer
# who is benefitted/harmed?
surv$TradeOpenCorpsBenII <- ""; 
for(i in 1:nrow(surv)){surv$TradeOpenCorpsBenII[i] <- surv[i,6:11][surv[i,6:11] !=""]}
surv$TradeOpenCorpsHarmII <- ""; for(i in 1:nrow(surv)){surv$TradeOpenCorpsHarmII[i] <- surv[i,12:17][surv[i,12:17] !=""]}
surv$TradeOpenCorpsBenII <- factor(surv$TradeOpenCorpsBenII, levels = 
  c("They are in industries where American companies sell their products outside the US",
    "They are in industries that face competition from foreign companies selling their products in the US",
    "They are large and very large companies","They are small and medium-sized companies",
    "All US companies benefit from trade","No US companies benefit from trade")
)
surv$TradeOpenCorpsHarmII <- factor(surv$TradeOpenCorpsHarmII, levels = 
  c("They are in industries where American companies sell their products outside the US",
    "They are in industries that face competition from foreign companies selling their products in the US",
    "They are large and very large companies","They are small and medium-sized companies",
    "All US companies are harmed by trade","No US companies are harmed by trade")
)

## redefine or reorder some categorical variables
# race
race2 <- rep("Other non-white", nrow(surv))
race2[surv$ethnicity == 1] <- "White"
race2[surv$ethnicity == 2] <- "Black"
race2[surv$hispanic %in% c(1,15) == FALSE] <- "Latino"
race2[surv$ethnicity %in% c(3:14)] <- "AAPI"
surv$race <- factor(race2, levels = c("White","Black","Latino","AAPI","Other non-white"))

# gender
gender2 <- rep("Female", nrow(surv))
gender2[surv$gender == 1] <- "Male"
surv$gender <- factor(gender2, levels = c("Female","Male"))

## create numeric versions of variables
# support for trade and trade agreements
surv$TradeOpenFavorNum <- NA; surv$TradeOpenFavorNum[surv$TradeOpenFavor == "Oppose a great deal"] <- 1
surv$TradeOpenFavorNum[surv$TradeOpenFavor == "Oppose somewhat"] <- 2; surv$TradeOpenFavorNum[surv$TradeOpenFavor == "Neither favor nor oppose"] <- 3
surv$TradeOpenFavorNum[surv$TradeOpenFavor == "Favor somewhat"] <- 4; surv$TradeOpenFavorNum[surv$TradeOpenFavor == "Favor a great deal"] <- 5

surv$TradeAgreementFavorNum <- NA; surv$TradeAgreementFavorNum[surv$TradeAgreementFavor == "Oppose a great deal"] <- 1
surv$TradeAgreementFavorNum[surv$TradeAgreementFavor == "Oppose somewhat"] <- 2; surv$TradeAgreementFavorNum[surv$TradeAgreementFavor == "Neither favor nor oppose"] <- 3
surv$TradeAgreementFavorNum[surv$TradeAgreementFavor == "Favor somewhat"] <- 4; surv$TradeAgreementFavorNum[surv$TradeAgreementFavor == "Favor a great deal"] <- 5

# party and ideology, turn to numeric scales
surv$party <- factor(surv$party, levels = c("Strong Democrat","Weak Democrat","Independent - leaning Democrat",
  "Independent - no preference","Independent - leaning Republican","Weak Republican","Strong Republican"))
surv$party <- as.numeric(surv$party)
surv$ideology <- factor(surv$ideology, levels = c("Extremely liberal","Liberal","Slightly liberal","Moderate; middle of the road",
  "Slightly conservative","Conservative","Extremely conservative"))
surv$ideology <- as.numeric(surv$ideology)
surv$income <- as.numeric(surv$hhi)
surv$income[surv$income == -3105] <- NA

# age
surv$age <- as.numeric(surv$age)

# collapse education to numeric binary
surv$college <- as.numeric(surv$education %in% c(4,5,6,7,8))
surv$college[surv$college == -3105] <- NA

# collapse employed into smaller number of categories and make numeric
surv$employed <- as.numeric(surv$employment %in% c("Employed full time","Employed part time"))
surv$unemployed <- as.numeric(surv$employment %in% c("Unemployed looking for work","Unemployed not looking for work"))
surv$retstudis <- as.numeric(surv$employment %in% c("Retired","Student","Disasbled"))

# iso; nat; trade effect; offshore effect.
surv$isolationism <- factor(surv$isolationism, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
surv$isolationism <- as.numeric(surv$isolationism)
surv$nationalism <- factor(surv$nationalism, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
surv$nationalism <- as.numeric(surv$nationalism)
surv$trade_affect <- factor(surv$trade_affect, levels = c("Negatively affected","Not affected","Positively affected"))
surv$trade_affect <- as.numeric(surv$trade_affect)
surv$offshore_affect <- factor(surv$offshore_affect, levels = c("Negatively affected","Not affected","Positively affected"))
surv$offshore_affect <- as.numeric(surv$offshore_affect)

# create cleaned version of the data for analysis
surv <- surv[c("TradeOpenFavorNum","TradeAgreementFavorNum",
  "TradeOpenCorpsBenII","TradeOpenCorpsHarmII",
  "age","gender","race",
  "college","income","employed","unemployed",
  "party","ideology",
  "isolationism","nationalism","trade_affect","offshore_affect"
)]

num_vars <- c("TradeOpenFavorNum","TradeAgreementFavorNum",
  "age",  
  "college","income","employed","unemployed",
  "party","ideology",
  "isolationism","nationalism","trade_affect","offshore_affect")
surv[,num_vars] <- apply(surv[,num_vars], 2, as.numeric)

###################
## Main analysis ##
###################

# distribution of responses on "who benefits"
table(surv$TradeOpenCorpsBenII)/sum(table(surv$TradeOpenCorpsBenII))
levels(surv$TradeOpenCorpsBenII)
levels(surv$TradeOpenCorpsHarmII)
ans1 <- as.numeric(surv$TradeOpenCorpsBenII)
ans2 <- as.numeric(surv$TradeOpenCorpsHarmII)

crosstab <- table(ans1,ans2)
crosstab <- apply(prop.table(crosstab),1,myround,3)
crosstab <- rbind(crosstab,myround(table(ans1)/sum(table(ans1)),3))
crosstab <- cbind(crosstab, c(myround(table(ans2)/sum(table(ans2)),3),""))

co=melt(crosstab)
head(co); co$value <- as.numeric(as.character(co$value))
co$Var2[43:49] <- 7; 

bins.cross <- seq(0,.15,by = .15/6)
bins.marg <- seq(0,.35,by = .35/6)

grays <- brewer.pal(9,"Blues")
purples <- brewer.pal(9,"Purples")

pdf("paper/figures/FigureA1.pdf", height = 5, width = 5)
par(mar = c(0, 1, 1, 0)) # Set the margin on all sides to 2
plot(-5,-5, xlim = c(0,7.3),ylim=c(7.3,0), axes = F, xlab = "", ylab = "")
adjx <- 0; adjy <- 0
for(i in 1:nrow(co)){
  # i <- 42
  if(co$Var1[i] %in% 1:6 & co$Var2[i] %in% 1:6){
    col <- grays[max(c(1:7)[co$value[i] > bins.cross])]
  }
  if(co$Var1[i] == 7 | co$Var2[i] == 7){
    col <- purples[max(c(1:7)[co$value[i] > bins.marg])]
  }
  adjx <- 0; adjy <- 0
  if(co$Var2[i] == 7) {adjx <- .15}
  if(co$Var1[i] == 7) {adjy <- .15}
  rect(co[i,"Var2"]-1 + adjx, co[i,"Var1"]-1+ adjy, co[i,"Var2"] + adjx, co[i,"Var1"]+ adjy, 
     col = col, border = "white", lwd = 3)
  if((co$Var1[i] == 7 & co$Var2[i] == 7) == FALSE){ 
    if(co$Var1[i] == 7 | co$Var2[i] == 7){ textcol <- ifelse(co$value[i] > .25,"white","black") }
    if(co$Var1[i] %in% 1:6 & co$Var2[i] %in% 1:6){ textcol <- ifelse(co$value[i] > .10,"white","black") }
    text(co[i,"Var2"]-.5 + adjx,co[i,"Var1"]-.5+ adjy, label = myround(co$value[i],2), col = textcol,
      cex = .8)
  }
}

axis(3, at=c(seq(.5,5.5,1),6.6), tick = F,
  labels=c("Exporting","Import-facing","Big firms","Small firms","All firms","No firms",""),
  cex.axis = .5, padj = 6)
axis(2, at=c(seq(.5,5.5,1),6.6), tick = F,
  labels=c("Exporting","Import-facing","Big firms","Small firms","All firms","No firms",""),
  cex.axis = .5, padj = 6)
mtext(side=3, line=0.1, "Which businesses benefit from trade?", cex = .8, adj = .4)
mtext(side=2, line=0.1, "Which businesses are harmed by trade?", cex = .8, adj = .7)

dev.off()

#######################
## Regression models ##
#######################

mod1 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies"), data = surv); 
mod2 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race, data = surv); 
mod3 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed, data = surv); 
mod4 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = surv); 
mod5 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = surv);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2:18], "(Intercept)")
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)] <- 
  c("Large firms benefit","Age","Male","Black","Latino","AAPI","Other non-white","College-educated","Income","Employed","Unemployed",
    "Party (D=1,R=7)","Ideology (L=1,C=7)","Isolationism","Nationalism","Trade-affected","Offshoring-affected","Intercept") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(36,37); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA1.tex")

mod1 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies"), data = surv); 
mod2 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race, data = surv); 
mod3 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed, data = surv); 
mod4 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = surv); 
mod5 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = surv);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2:18], "(Intercept)")
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)] <- 
  c("Large firms benefit","Age","Male","Black","Latino","AAPI","Other non-white","College-educated","Income","Employed","Unemployed",
    "Party (D=1,R=7)","Ideology (L=1,C=7)","Isolationism","Nationalism","Trade-affected","Offshoring-affected","Intercept") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(36,37); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA2.tex")

## analysis
mod1 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies"), data = surv); 
mod2 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race, data = surv); 
mod3 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed, data = surv);
mod4 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = surv); 
mod5 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = surv); 

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2:18], "(Intercept)")
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)] <- 
  c("SMEs are harmed","Age","Male","Black","Latino","AAPI","Other non-white","College-educated","Income","Employed","Unemployed",
    "Party (D=1,R=7)","Ideology (L=1,C=7)","Isolationism","Nationalism","Trade-affected","Offshoring-affected","Intercept") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(36,37); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA3.tex")

mod1 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies"), data = surv);
mod2 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race, data = surv); 
mod3 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed, data = surv);
mod4 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = surv); 
mod5 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = surv); 

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2:18], "(Intercept)")
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)] <- 
  c("SMEs are harmed","Age","Male","Black","Latino","AAPI","Other non-white","College-educated","Income","Employed","Unemployed",
    "Party (D=1,R=7)","Ideology (L=1,C=7)","Isolationism","Nationalism","Trade-affected","Offshoring-affected","Intercept") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(36,37); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA4.tex")

## One-on-one comparisons
# analysis
cur <- surv[surv$TradeOpenCorpsBenII %in% levels(surv$TradeOpenCorpsBenII)[c(1,3)],]
mod1 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies"), data = cur); 
mod2 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race, data = cur); 
mod3 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed, data = cur); 
mod4 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = cur); 
mod5 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = cur);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Large firms benefit") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA1_bottom.tex")

mod1 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies"), data = cur); 
mod2 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race, data = cur); 
mod3 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed, data = cur); 
mod4 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = cur);
mod5 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsBenII == "They are large and very large companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = cur); 

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("Large firms benefit") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA2_bottom.tex")

## analysis
cur <- surv[surv$TradeOpenCorpsHarmII %in% levels(surv$TradeOpenCorpsHarmII)[c(2,4)],]
mod1 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies"), data = cur); 
mod2 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race, data = cur); 
mod3 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed, data = cur); 
mod4 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = cur);
mod5 <- lm(TradeOpenFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = cur);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("SMEs are harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA3_bottom.tex")

mod1 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies"), data = cur);
mod2 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race, data = cur); 
mod3 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed, data = cur);
mod4 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = cur); 
mod5 <- lm(TradeAgreementFavorNum ~ I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") + age + gender + race + college + income + employed + unemployed + 
  party + ideology + isolationism + nationalism + trade_affect + offshore_affect, data = cur); 

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef, summary(mod5)$coef)
vars <- c(rownames(mods[[5]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("SMEs are harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]),sum(summary(mod5)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableA4_bottom.tex")





